<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\xselect;

use Yii;
use builder\form\util\control\Control;

/**
 * xselect （xm-select）
 * @desc 该控件用于补充select2相关控件的不足
 * @author cleverstone
 * @since ym2.0
 * @see https://maplemei.gitee.io/xm-select
 */
class Xselect extends Control
{
    /**
     * @var array
     */
    protected $options = [
        'control' => 'xselect',
        'label' => '',
        'required' => false,
        'class' => '',
        'attribute' => '',
        'comment' => '',
        'layout' => 12,
        'pluginOptions' => [
            'data' => [],
            'initValue' => null,
            'tips' => '请选择',
            'filterable' => false,//是否开启搜索
            'searchTips' => '请选择',//搜索提示
            'delay' => 100,//搜索延迟
            'radio' => false,//是否单选，插件默认多选
            'repeat' => false,//是否开启重复性模式
            'clickClose' => false,//是否点击选项后自动关闭下拉框
            'autoRow' => false,//是否自动换行
            'size' => 'small',//尺寸
        ],
    ];

    /**
     * 是否开启分页
     * @param boolean $paging
     * @return $this
     */
    public function paging($paging)
    {
        $this->options['pluginOptions']['paging'] = (boolean)$paging;
        return $this;
    }

    /**
     * 工具项
     * @param array $toolbar
     * @return $this
     */
    public function toolbar(array $toolbar)
    {
        $this->options['pluginOptions']['toolbar'] = $toolbar;
        return $this;
    }

    /**
     * 搜索延迟
     * @param int $millisecond 毫秒
     * @return $this
     */
    public function delay($millisecond)
    {
        $this->options['pluginOptions']['delay'] = (int)$millisecond;
        return $this;
    }

    /**
     * 设置数据选项
     *
     * 参数           说明                   类型    可选值       默认值
     * name        显示名称                 string    -          name
     * value       选中值, 当前多选唯一      string    -          value
     * selected    是否选中                 string    -         selected
     * disabled    是否禁用                 string    -         disabled
     * children    分组children             string    -         children
     * optgroup    分组optgroup            string     -         optgroup
     *
     * @param array $data
     * @return $this
     */
    public function data(array $data)
    {
        $this->options['pluginOptions']['data'] = $data;
        return $this;
    }

    /**
     * 默认值
     * @param string|array $defaultValue
     * @return $this
     */
    public function defaultValue($defaultValue)
    {
        $this->options['pluginOptions']['initValue'] = (array)$defaultValue;
        return $this;
    }

    /**
     * 占位提示
     * @param string $tips
     * @return $this
     */
    public function tips($tips)
    {
        $this->options['pluginOptions']['tips'] = (string)$tips;
        return $this;
    }

    /**
     * 开启搜索
     * @return $this
     */
    public function filterable()
    {
        $this->options['pluginOptions']['filterable'] = true;
        return $this;
    }

    /**
     * 搜索提示
     * @param string $tips
     * @return $this
     */
    public function searchTips($tips)
    {
        $this->options['pluginOptions']['searchTips'] = (string)$tips;
        return $this;
    }

    /**
     * 单选
     * @return $this
     */
    public function radio()
    {
        $this->options['pluginOptions']['radio'] = true;
        return $this;
    }

    /**
     * 开启重复选
     * @return $this
     */
    public function repeat()
    {
        $this->options['pluginOptions']['repeat'] = true;
        return $this;
    }

    /**
     * 点击后，自动关闭下拉内容框
     * @return $this
     */
    public function clickClose()
    {
        $this->options['pluginOptions']['clickClose'] = true;
        return $this;
    }

    /**
     * 自动换行
     * @return $this
     */
    public function autoRow()
    {
        $this->options['pluginOptions']['autoRow'] = true;
        return $this;
    }

    /**
     * 新增配置项
     * @param string $field
     * @param mixed $value
     * @return $this
     */
    public function addPluginOption($field, $value)
    {
        $this->options['pluginOptions'][$field] = $value;
        return $this;
    }

    /**
     * 标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->options['label'] = (string)$label;
        return $this;
    }

    /**
     * 必填
     * @return $this
     */
    public function required()
    {
        $this->options['required'] = true;
        return $this;
    }

    /**
     * CSS类
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->options['attribute'] = $attribute;

        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * 布局，1-12
     * @param int $layout
     * @return $this
     */
    public function layout($layout)
    {
        $this->options['layout'] = (int)$layout;
        return $this;
    }

    /**
     * {{@inheritdoc}}
     */
    public function _toArray()
    {
        $options = $this->options;
        $viewParams = Yii::$app->view->params;
        if (
            !isset($options['pluginOptions']['theme'])
            && !empty($viewParams['themeColor'])
        ) {
            $options['pluginOptions']['theme'] = ['color' => $viewParams['themeColor']];
        }

        return $options;
    }
}